﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KUMC.Neurology.SomatoTopo
{
    public partial class AddSubjectForm : Form
    {
        /*
         * Constructor
         * */
        public AddSubjectForm()
        {
            InitializeComponent();
        }
        public AddSubjectForm(Layer template)
            : this()
        {
            this.template = template;
            this.selection = new Selection(template.Size);
            this.distribution = new Layer(template.Size);

            this.selection.SetPen(Color.Red);
        }

        /*
         * Button
         * */
        private void btSubjectCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        /*
         * Event
         * */
        private void pbSubject_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(this.disp_tem, 0, 0);
            e.Graphics.DrawImage(this.disp_dis, 0, 0);
            e.Graphics.DrawImage(this.disp_sel, 0, 0);
        }
        private void pbSubject_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.SelectRegion_Line(e.Location);
                this.GetScaleFormVsCanvas();
                pbSubject.Invalidate();
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.distribution.FillRegion(this.selection.GetRegion());
                this.GetScaleFormVsCanvas();
                pbSubject.Invalidate();
                this.selection.Clear();
            }
        }
        private void AddSubjectForm_Resize(object sender, EventArgs e)
        {
            this.SetControlPosition();
            this.GetScaleFormVsCanvas();
            this.pbSubject.Invalidate();
        }
        private void AddSubjectForm_Load(object sender, EventArgs e)
        {
            this.SetControlPosition();
            this.GetScaleFormVsCanvas();
            this.pbSubject.Invalidate();
        }

        /*
         * Methods
         * */
        private void SetControlPosition()
        {
            Rectangle formrect = this.ClientRectangle;
            int margin = 12;

            pbSubject.Width = formrect.Width - (margin * 2);
            pbSubject.Height = formrect.Height - (margin * 3) - pnTBGroup.Height;

            pnTBGroup.Location = new Point(margin, formrect.Height - margin - pnTBGroup.Height);
            pnBTNGroup.Location = new Point(formrect.Width - margin - pnBTNGroup.Width,
                                            formrect.Height - margin - pnBTNGroup.Height);
        }
        private void GetScaleFormVsCanvas()
        {
            if (this.template.Size.Width < this.template.Size.Height)
            {
                if ((double)this.ClientRectangle.Width < (double)this.template.Size.Width)
                {
                    this.scale = (double)this.ClientRectangle.Width / (double)this.template.Size.Width;
                }
                else
                {
                    this.scale = (double)this.ClientRectangle.Height / (double)this.template.Size.Height;
                }
            }
            else
            {
                if ((double)this.ClientRectangle.Height < (double)this.template.Size.Height)
                {
                    this.scale = (double)this.ClientRectangle.Height / (double)this.template.Size.Height;
                }
                else
                {
                    this.scale = (double)this.ClientRectangle.Width / (double)this.template.Size.Width;
                }
            }

            this.disp_tem = this.template.ZoomInOut(this.scale);
            this.disp_dis = this.distribution.ZoomInOut(this.scale);
            this.disp_sel = this.selection.ZoomInOut(this.scale);
        }
        private void SelectRegion_Line(Point point)
        {
            int x = (int)(point.X / this.scale);
            int y = (int)(point.Y / this.scale);

            this.selection.SelectRegion(new Point(x, y));
        }


        /*
         * Getter
         * */
        public string SubjectName { get { return this.tbSubjectName.Text; } }
        public string SubjectSex { get { return this.tbSex.Text; } }
        public string SubjectAge { get { return this.tbAge.Text; } }
        public string SubjectOption { get { return this.tbOption.Text; } }
        public Layer SubjectDistribution { get { return this.distribution; } }

        /*
         * Member variable
         * */
        private Layer template = null;
        private Selection selection = null;
        private Layer distribution = null;

        private Bitmap disp_tem = null;
        private Bitmap disp_dis = null;
        private Bitmap disp_sel = null;

        private double scale;
    }
}
